package com.example.project.projectspringcloud.controller.admin.discountcoupon;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.discountcoupon.DiscountCoupon;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.service.discountcoupon.DiscountCouponService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.StringUtil;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = "后台-用户优惠券管理")
@Controller
@RequestMapping(value = "/admin/discountcoupon")
public class AdminDiscountCouponController {

    @Autowired
    private DiscountCouponService discountCouponService;

    @Autowired
    private UserService userService;

    private static final Set<BigDecimal> prices = Sets.newTreeSet();

    static {
        prices.add(new BigDecimal(10.00));
        prices.add(new BigDecimal(20.00));
        prices.add(new BigDecimal(30.00));
        prices.add(new BigDecimal(50.00));
        prices.add(new BigDecimal(100.00));
        prices.add(new BigDecimal(200.00));
    }

    @ApiOperation(value = "返回列表", notes = "返回优惠券列表")
    @GetMapping(value = {"", "/list"})
    public String list(DiscountCoupon discountCoupon, Page page, Model model) {
        List<DiscountCoupon> discountCoupons = discountCouponService.list(new Page(), discountCoupon);
        model.addAttribute("page", new Page<DiscountCoupon>(discountCoupons));
        model.addAttribute("discountCoupon", discountCoupon);
        return "admin/discountcoupon/discountcoupon-list";
    }

    @ApiOperation(value = "表单", notes = "返回表单")
    @GetMapping(value = "/form")
    public String form(@RequestParam(value = "id", required = false) Integer id, Model model) {
        if (id != null) {
            DiscountCoupon entity = new DiscountCoupon();
            entity.setId(id);
            entity = discountCouponService.get(entity);
            model.addAttribute("discountcoupon", entity);
        }

        List<User> users = userService.list(null, new User());
        model.addAttribute("users", users);

        model.addAttribute("prices", prices);

        return "admin/discountcoupon/discountcoupon-form";
    }

    @ApiOperation(value = "保存数据", notes = "根据ID判断更新/插入")
    @PostMapping(value = "/api/save")
    @ResponseBody
    public RespBody save(DiscountCoupon discountCoupon) throws Exception {
        try {
            boolean result = discountCouponService.save(discountCoupon);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    @ApiOperation(value = "删除优惠券", notes = "根据ids删除优惠券")
    @DeleteMapping(value = "/api/delete/{ids}")
    @ResponseBody
    public RespBody delete(@ApiParam(value = "id/ids 1,2,3...") @PathVariable(value = "ids") String ids) {
        discountCouponService.delete(StringUtil.split(ids, ","));
        return RespBody.ok(true);
    }

}
